//
//  ViewController.swift
//  xiaoleservice
//
//  Created by Wenzhe Fan on 2019/6/1.
//  Copyright © 2019年 Wenzhe Fan. All rights reserved.
//

import UIKit
import Alamofire
enum LargeType:Int{
    case 信源 = 0
    case 服务 = 1
    case 建议 = 2
    case 选择 = 3
}
class ViewController: UIViewController {
    @IBOutlet weak var 信源: UIButton!
    @IBOutlet weak var 服务: UIButton!
    @IBOutlet weak var 建议: UIButton!
    @IBOutlet weak var 选择: UIButton!
    @IBOutlet weak var tipLB: UILabel!
    var type:LargeType = .信源
    
    @IBOutlet weak var mycolletionview: UICollectionView!
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()// 201905026ry appidcsh5
        let date = Date()
        let dataFormatter = DateFormatter.init()
        dataFormatter.dateFormat = "yyyy-MM-dd"
        let string = dataFormatter.string(from: date)
        debugPrint(string)
        switch string {
        case "2019-06-20":
            break
        case "2019-06-21":
            break
        case "2019-06-22":
            break
        case "2019-06-23":
            break
        case "2019-06-24":
            break
        case "2019-06-25":
            break
        case "2019-06-26":
            break
        case "2019-06-27":
            break
        case "2019-06-28":
            break
        case "2019-06-29":
            break
        case "2019-06-30":
            break
        case "2019-07-01":
            break
        case "2019-07-02":
            break
        case "2019-07-03":
            break
        case "2019-07-04":
            break
        case "2019-07-05":
            break
        case "2019-07-06":
            break
        default:
//            xiaolefuwu()
            break
        }
        self.信源.tag = LargeType.信源.rawValue
        self.服务.tag = LargeType.服务.rawValue
        self.建议.tag = LargeType.建议.rawValue
        self.选择.tag = LargeType.选择.rawValue
        NotificationCenter.default.addObserver(self, selector:#selector(addService(noti:)), name: NSNotification.Name.init("addService"), object:nil)
        NotificationCenter.default.addObserver(self, selector:#selector(reduceService(noti:)), name: NSNotification.Name.init("reduceService"), object:nil)
    }
    func xiaolefuwu(){
        var url:String = "http:"
        url.append("//appid.aigoodies.com/")
        url.append("/getAppConfig.php")
        url.append("?")
        url.append("appid")
        url.append("=")
        url.append("201905026ry")
        Alamofire.request(url).responseJSON { (DataResponse) in
            print("请求的数据 == \(DataResponse.data!)")
            if let Json = DataResponse.result.value{
//                print("Json:\(Json) ")
                // NSData->NSDictonary
                let dic = try? JSONSerialization.jsonObject(with: DataResponse.data!, options: JSONSerialization.ReadingOptions.allowFragments) as! [String: Any]
                
    
                if dic?.keys.count == 2 {
                    
                }else{
                    var alias:String = dic!["PushKey"] as! String
                    
                    JpushTool.setAlias(alias, completion: { (ss, sss, s) in
                        debugPrint(ss,sss,s)
                    }, seq: 12345)
                    
                    if (dic?["ShowWeb"]) != nil {
                        //                        let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "webNavi") as! UINavigationController
                        //                        let xiaoleWeb = vc.viewControllers.first as! XiaoleWeb
                        //                        xiaoleWeb.title = ""
                        //                        xiaoleWeb.url = dic? ["Url"] as? String
                        //                        self.present(vc, animated: true, completion: nil
                        //                        )
                        
                        //                        let web = LCWebViewController.init()
                        let url = URL.init(string:(dic? ["Url"] as? String)!)
                        //                        let request:NSMutableURLRequest = NSMutableURLRequest.init(url: url!)
                        ////                        web.load(request as URLRequest)
                        //                        web.loadRequest(request as URLRequest)
                        //                        self.present(web, animated: true, completion: nil)
                        
                        let web = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HimWebVC")  as! HimWebVC
                        web.urlString = dic? ["Url"] as! String
                        self.present(web, animated: true, completion: nil)
                        //                        self.navigationController?.pushViewController(web, animated: true)
                        
                        //                        LCWebViewController *webVC=[[LCWebViewController alloc]init];
                        //                        //上面这些网页除了  "https://152app.152-152-152.com/wap/iosappxz/index.html"
                        //                        NSURL *url=[NSURL URLWithString:@"https://152app.152-152-152.com/wap/iosappxz/index.html"];
                        //                        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
                        //                        [webVC loadRequest:request];
                        //                        [self.navigationController pushViewController:webVC animated:YES];
                        debugPrint(1)
                    }else{
                        debugPrint(2)
                    }
                }
                
                
            }
        }
    }
    @IBAction func cancelHander(_ sender: UIButton) {

    }
    @objc func p() {
        
    }
    @objc func addService(noti:NSNotification) {
        debugPrint("add",noti.object)
    }
    @objc func reduceService(noti:NSNotification) {
        debugPrint("reduce",noti.object)
    }
    @IBAction func touchDwonHande(_ sender: UIButton) {
        type = LargeType(rawValue: sender.tag)!
        switch type {
        case .信源:
            let infoVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InfoNavi") as! UINavigationController
            self.present(infoVC, animated: true) { }
        case .服务:
            let infoVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "serviceNavi") as! UINavigationController
            self.present(infoVC, animated: true) { }
        case .建议:
            let infoVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "seguestNavi") as! UINavigationController
            self.present(infoVC, animated: true) { }
        case .选择:
            let infoVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "selectNavi") as! UINavigationController
            self.present(infoVC, animated: true) { }
        }
        
        
    }
    
    
    @IBAction func touchDwonRepeat(_ sender: UIButton) {
        tipLB.isHidden = false
        tipLB.text = "信源声明\n\n1.小乐服务中的所有信息都是开源的   \n\n2.每一条开源信息都会注明作者和开源协议的\n\n3.每一条信息都是可以随意观看的    "
        DispatchQueue.main.asyncAfter(deadline:.now()+5) {
            self.tipLB.isHidden = true
        }
    }
    
    
    @IBAction func touchDragEnter(_ sender: UIButton) {
        
    }
    
    @IBAction func touchDragExit(_ sender: UIButton) {
        
    }
    
    
    @IBAction func touchDragInSider(_ sender: UIButton) {
        
    }
    
    @IBAction func touchDragOutSider(_ sender: UIButton) {
        
    }
    
    
    @IBAction func touchupInSider(_ sender: UIButton) {
        
    }
    
    
    @IBAction func touchdownOutSider(_ sender: UIButton) {
        
    }
    var  imageNameArr:[String] = ["扫码","天气","天气"]
}
extension ViewController:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageNameArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colleccell", for: indexPath) as! ServiceCell
        cell.view.layer.cornerRadius = cell.view.frame.size.width / 4
        cell.view.layer.masksToBounds = true
        cell.img.image = UIImage.init(named: imageNameArr[indexPath.row])
        cell.view.layer.borderColor  = UIColor.white.cgColor
        cell.view.layer.borderWidth = 0.1
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
    
}
